//
//  SLContactInformationTableViewCell.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/22.
//

#import "SLContactInformationTableViewCell.h"
#import <YYKit.h>
#import <Masonry.h>
#import "SLInformationOptionComponent.h"
#import "DashedLineView.h"

@interface SLContactInformationTableViewCell ()
@property (nonatomic, strong) UIView *bgView;// 容器
@property (nonatomic, strong) UIImageView *decorate;
@property (nonatomic, strong) UIStackView *stackView;// 容器
@end
@implementation SLContactInformationTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = UIColor.clearColor;
        self.contentView.backgroundColor = UIColorHex(#F5F5F5);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.bgView];
        [self.contentView addSubview:self.decorate];
        
        [self.bgView addSubview:self.stackView];
        
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView).inset(0);
            make.top.equalTo(self.contentView).offset(25*0.5);
            make.bottom.equalTo(self.contentView);
        }];
        [self.decorate mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.bgView).inset(20);
            make.centerY.equalTo(self.bgView.mas_top);
            make.height.mas_equalTo(25);
        }];
        [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgView).offset(34);
            make.left.right.equalTo(self.bgView).inset(35);
            make.bottom.equalTo(self.bgView).inset(155);
        }];
    }
    return self;
}

- (void)setItems:(NSArray *)items {
    _items = items;
    [self.stackView removeAllSubviews];
    
    for (NSDictionary *dict in items) {
        UIView *itemContainer = [UIView new];
        [self.stackView addArrangedSubview:itemContainer];
        
        SLInformationOptionComponent *item1 = [self optionItem];
        item1.textField.enabled = NO;
        [itemContainer addSubview:item1];
        
        SLInformationOptionComponent *item2 = [self optionItem];
        item2.textField.enabled = NO;
        item2.more.image = [UIImage imageNamed:@"icon_contact"];
        [itemContainer addSubview:item2];
        
        // 事件
        [item1 jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            if (self.itemRelationClickCallback) {
                self.itemRelationClickCallback([items indexOfObject:dict]);
            }
        }];
        [item2 jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            if (self.itemContactClickCallback) {
                self.itemContactClickCallback([items indexOfObject:dict]);
            }
        }];
        // 赋值
        NSArray *catholicism = dict[@"catholicism"]; // - 关系选项的列表
        NSString *relationship = nil;
        for (NSDictionary *it in catholicism) {
            if ([it[@"peppers"] sl_intValue] == [dict[@"asleep"] sl_intValue]) {
                relationship = it[@"veins"];
                break;
            }
        }
        item1.titleLabel.text = dict[@"riding"];
        item1.textField.text = relationship;
        item1.textField.placeholder = dict[@"praying"] ?: @"Choose Your Relationship";
        
        NSMutableArray *arr = [NSMutableArray array];
        NSString *veins = dict[@"veins"];
        NSString *ass = dict[@"ass"];
        if (veins.length > 0) [arr addObject:veins];
        if (ass.length > 0) [arr addObject:ass];
        item2.titleLabel.text = dict[@"vexed"];
        item2.textField.text = [arr componentsJoinedByString:@"-"];
        item2.textField.placeholder = dict[@"priest"] ?: @"Choose Your Contact";
        item2.more.image = [UIImage imageNamed:@"icon_contact"];
        
        
        DashedLineView *line = [DashedLineView new];
        [itemContainer addSubview:line];
        
        [item1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(item1.superview);
            make.top.equalTo(item1.superview);
            make.height.mas_equalTo(65);
        }];
        [item2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(item2.superview);
            make.top.equalTo(item1.mas_bottom).offset(19);
            make.height.mas_equalTo(item1);
        }];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(item2.mas_bottom).offset(15);
            make.height.mas_equalTo(2);
        }];
        if ([items indexOfObject:dict] == items.count-1) {
            line.hidden = YES;
        }
        [itemContainer mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(65*2+30);
        }];
    }
}

- (SLInformationOptionComponent *)optionItem {
    SLInformationOptionComponent *item = [SLInformationOptionComponent new];
    return item;
}
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.layer.cornerRadius = 20;
        _bgView.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _bgView.layer.borderWidth = 1.5;
    }
    return _bgView;
}
- (UIImageView *)decorate {
    if (!_decorate) {
        _decorate = [UIImageView new];
        _decorate.contentMode = UIViewContentModeScaleAspectFit;
        _decorate.image = [UIImage imageNamed:@"icon_circle_bar"];
    }
    return _decorate;
}

- (UIStackView *)stackView {
    if (!_stackView) {
        _stackView = [UIStackView new];
        _stackView.spacing = 19;
        _stackView.axis = UILayoutConstraintAxisVertical;
    }
    return _stackView;
}

@end
